<?php

//------------------------------------------------------------------------------

// Class              : Ctl_AlbumSanpham

// Modification       :

//------------------------------------------------------------------------------

// Date(yyyy.mm.dd)   Developer                       Action

//------------------------------------------------------------------------------

// 2007.03.09         Oanhhtk                         Created

//------------------------------------------------------------------------------



class Ctl_AlbumSanpham extends ControlBase{

    var $modul="album_project";

	//==========================================================================

	// Function    : Ctl_AlbumSanpham

	// Creator     : Oanhhtk 2007.03.09

	// Details     : Call the constructor of father class to initial variable

	//==========================================================================

	function Ctl_AlbumSanpham(){

         // Constructor of father class.

	   	 ControlBase::ControlBase();

	}



	//==========================================================================

	// Function    : execute

	// Argument    :

	// ReturnValue :

	// Creator     : Oanhhtk 2007.03.09

	// Details     : Execute the process depend the given mode, 

    //               as: Add, Delete, Edit and default is List.

	//==========================================================================

 	function execute($action=""){

	    global $db;

	    global     $arrayError,$_POST;

	    $arrayData =  array();

        // Get back the work you want to do.

	    $this->modul = CM_getParameter(MODUL);

 		if ($action==""){

            // Get back the action: as: Add, Delete, Edit or List.

 			$action = CM_getParameter(ACTION);

 		}

        

		// Get data

		include_once(DIR_MODEL."md_album_san_pham.class.php");

		$objAlbumSanpham = new MD_AlbumSanpham($this->db);

        $sanpham_id = CM_getParameter("sanpham_id");

 		switch($action){

			case "Add":

				if(!isset($_POST['Submit']))

				{

					

                   // show form add new

       				include_once(DIR_VIEW."v_album_san_pham.php");

					V_AlbumSanphamEdit($arrayData,"Add");

				}

				else

				{

					// check validating

                    if($this->validate() && $this->insert())

					{

                     	CM_gotoPage("?".MODUL."=".$this->modul."&".ACTION."=list&sanpham_id=".$sanpham_id);

                    }

                    else

					{

         				include_once(DIR_VIEW."v_album_san_pham.php");

						V_AlbumSanphamEdit(&$_POST,"Add",$sanpham_id);

                    }



				}

				break;

			case "Delete":

				$id = CM_getParameter("id");

   				$this->deleteById($id);

				CM_gotoPage("?".MODUL."=".$this->modul."&".ACTION."=list&sanpham_id=".$sanpham_id);             

				break;

			case "Edit":

				include_once(DIR_VIEW."v_album_san_pham.php");

 				$id = CM_getParameter("id");

				if(isset($_POST['submitanh'])){

                    $this->updateanh();

				}

				if(isset($_POST['Submit'])){

                    if($this->validate() && $this->update()){

		                $arrayData = $objAlbumSanpham->getByWhere("*","id_sanpham = '".$sanpham_id."'","ngay_tao desc");

                	 	// show data

						include_once(DIR_VIEW."v_album_san_pham_list.php");

						V_AlbumSanphamList($arrayData);

                    }

                    else{

                    	$arrayData =  &$_POST;

						V_AlbumSanphamEdit($arrayData,"Edit",$sanpham_id);

                    }

				}else{

                     $arrayData = $objAlbumSanpham->getById2($id);

                     if(empty($arrayData)){

                         $arrayError[]="Record not found."   ;

                     }

                     V_AlbumSanphamEdit($arrayData,"Edit");

				}

				break;

		

           case "View":

                 // Get a record with given id

                 $id = CM_getParameter("id");

                 $arrayData = $objAlbumSanpham->getById2($id);

                 // Error processing

                 if(empty($arrayData)){

                    $arrayError[]="Không tìm thấy bản ghi nào có id=".$id;

                 } 

                 

                 $id_loai = $arrayData["id_loai"];

                 // Get all data for the list

                 $arrayDataLink = $objAlbumSanpham->getAll("*");

                 // Error processing

                 if ($objAlbumSanpham->errno()){

                    $arrayError[]= $objAlbumSanpham->error();

                 } 



                 // Show data

                 include_once(DIR_VIEW."v_du_an_view.php");                   

                 V_DuanView(&$arrayData, &$arrayDataLink, $id_loai);                                 

                 break;				

             case "Edit_ht":  

                $strID = CM_getParameter("id");   

                $strHienThi = CM_getParameter("hien_thi");   

                // Update data

                if (!$this->update_ht($strID,$strHienThi)){

                    $arrayError[]= " Có lỗi trong quá trình cập nhật dữ liệu.";

                }                       

			case "List":

			default : {

				$arrayData = $objAlbumSanpham->getByWhere("*","id_sanpham = '".$sanpham_id."'","ngay_tao desc");

				include_once(DIR_MODEL."md_san_pham.class.php");

				$objSanpham = new MD_Sanpham($this->db);

				$arrayDataCat = array();

				$arrayDataCat = $objSanpham->getByWhere("*","id = '".$sanpham_id."'","");

                 // show data

				include_once(DIR_VIEW."v_album_san_pham_list.php");

                //check loi

                if ($objAlbumSanpham->errno()){

                         $arrayError[]= $objAlbumSanpham->error();

                }                

				V_AlbumSanphamList($arrayData,$arrayDataCat);

			}

 		}



        unset($arrayData,$arrayDataCat);

	}



	//==========================================================================

	// Function    :

	// Argument    :

	// ReturnValue :

	// Creator     : Oanhhtk 2007.03.09

	// Details     : AppendData record of table

	//==========================================================================

 	function validate(){

        global     $arrayError,$_POST;

   		if (trim($_POST["tieu_de_anh"])=="") {

   			$arrayError[]="Tiêu đề ảnh không được rỗng.";

   			return false;

   		}

        return true;

	}



	//==========================================================================

	// Function    :

	// Argument    :

	// ReturnValue :

	// Creator     : Oanhhtk 2007.03.09

	// Details     : AppendData record of table

	//==========================================================================

 	function insert(){

		global     $arrayError,$_POST;
		
   		$objAlbumSanpham = new MD_AlbumSanpham($this->db);

   		$objAlbumSanpham->insert(&$_POST);

     	if ($objAlbumSanpham->errno()){

             $arrayError[]= $objAlbumSanpham->error();

             return false;

     	}

		//-----------------Goi ham upaloadfile ---------------------------------
		$dir = "../images/images_upload/img_album_san_pham/";
		$this->DoUploadFileImg($dir);
     	return true;

	}

  	//==========================================================================

	// Function    :

	// Argument    :

	// ReturnValue :

	// Creator     : Oanhhtk 2007.03.09

	// Details     : AppendData record of table

	//==========================================================================

 	function deleteById(){

        global     $arrayError,$_POST;

   		$objAlbumSanpham = new MD_AlbumSanpham($this->db);

   		$objAlbumSanpham->deleteById(CM_getParameter("id"));

     	if ($objAlbumSanpham->errno()){

             $arrayError[]= $objAlbumSanpham->error();

             return false;

     	}

     	return true;

	}

	//==========================================================================

	// Function    :

	// Argument    :

	// ReturnValue :

	// Creator     : Oanhhtk 2007.03.09

	// Details     : AppendData record of table

	//==========================================================================

 	function update(){

        global     $arrayError,$_POST, $_FILES;

		$objAlbumSanpham = new MD_AlbumSanpham($this->db);

   		$objAlbumSanpham->update(&$_POST);

     	if ($objAlbumSanpham->errno()){

             $arrayError[]= $objAlbumSanpham->error();

             return false;

     	}

        //-----------------Goi ham upaloadfile ---------------------------------------------------------------
		$dir = "../images/images_upload/img_album_san_pham/";
		$this->DoUploadFileImg($dir);

     	return true;

	}

	//==========================================================================

    // Function    : update_ht                                                   //

    // Creator     : Oanhhtk 2007.03.29                                        //

    // Details     : Update hien_thi field                                    //

    //==========================================================================

    function update_ht($strID, $strHienThi)

	{

        //global $arrayError, $_FILES;		

		global     $arrayError,$_POST;

		$objAlbumSanpham = new MD_AlbumSanpham($this->db);

        $objAlbumSanpham->update_ht($strID, $strHienThi);

        if ($objAlbumSanpham->errno()){

            $arrayError[]= $objAlbumSanpham->error();

            return false;

        }

        return true;

    }  

	//==========================================================================

	// Function    : updateanh

	// Argument    :

	// ReturnValue :

	// Creator     : Tran Thanh 2007.03.08

	// Details     : AppendData record of table

	//==========================================================================

 	function updateanh(){

        global     $arrayError,$_POST;

   		$objModel = new MD_AlbumSanpham($this->db);

   		$objModel->updateanh(&$_POST);

     	if ($objModel->errno()){

             $arrayError[]= $objModel->error();

             return false;

     	}

     	return true;

		

	}

    //-----------------------------------------------------------------                

    //  Fucntion : uploadfile()

    //  Outline  : upaloadfile to ../images folder

    //  Output   : 1 : OK ; 0: Error

    //  Creator  : Dang Thanh 2007.03.09

    //-----------------------------------------------------------------    

    function  CM_upload($duoiChoPhep='gif,bmp,jpg,GIF,BMP,JPG'){

        global $_FILES;

        global $HTTP_POST_VARS ;

        $duoi3 = substr($_FILES["ten_anh"]['name'],strlen($_FILES["ten_anh"]['name'])-3,3);

		

        // kiem tra

        if(! strstr($duoiChoPhep,$duoi3)) return false;

		

		$this->DoUploadFile1();

        return  true;

    }

       



}



  

?>